package com.mipo.client.javase.company.flytek.linyi.jxtauthourity;

import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class TestPorduct {
	public static void main(String[] args) throws Exception {
		String url1 = "http://www.linyi.net.cn/Admin.ResourceManage/index.php?app=User&mod=AssistantWarrant&act=addWarrant&id=4039"; // 追加授权
		String url2 = "http://www.linyi.net.cn/Admin.ResourceManage/index.php?app=User&mod=AssistantWarrant&act=addWarrantNew&loginName=370681197802021046"; // 新授权
		String url3 = "http://cyservice.edc.linyi.net.cn/api?appkey=C6SakiHO&access_token=changyanyun&ct=3&cver=4.0.4&method=sns.user.status&version=1.0&format=json&loginName=Sch12345"; // 查询状态
		String url = "http://linyi.book.changyan.cn/"; // 授权服务地址
		
		//authNew();
		//queryStatus();
		//queryTaocan();
	}
	
	// 新授权
	public static void authNew() throws Exception {
		String url = "http://linyi.book.changyan.cn/index.php?c=home&a=doRegisterForCustomizedNew&loginName=Sch12345&Product=ta_iflybook_linyi&provinceCode=1489&cityCode=1607&districtCode=1618&schoolId=4637130095000000260&cardId=03FE661E-0FDF-FCB5-51CB-DADA5CE81869&reqId=Sch123451513331292";
		excuteGetRequest(url);
		
		
	}
	
	// 查询状态
	public static void queryStatus() throws Exception {
		String url = "http://linyi.book.changyan.cn/index.php?c=home&a=getUserCardState&loginName=Sch12345&appName=rrt_linyi";
		String url2 = "http://linyi.book.changyan.cn/index.php?c=home&a=getUserCardState&loginName=lijie_test&appName=rrt_linyi";
		excuteGetRequest(url);
	}
	
	public static void queryTaocan()throws Exception{
		String url = "http://linyi.book.changyan.cn/index.php?c=home&a=getCardInfo&cardId=03FE661E-0FDF-FCB5-51CB-DADA5CE81869";
		excuteGetRequest(url);
	}
	
	public static void excuteGetRequest(String url) throws Exception {
		System.out.println("请求url："+url);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = httpClient.execute(httpGet); // 4.3版本不设置超时的话，一旦服务器没有响应，等待时间N久(>24小时)。
		HttpEntity entity = response.getEntity(); // 返回报文实体
		StatusLine status = response.getStatusLine();
		System.out.println("返回的状态码为：" + status); // 具体返回码(200)为status.getStatusCode();
		String ret = EntityUtils.toString(entity); // 将返回报文实体转换为字符串
		System.out.println("返回的报文为：" + URLDecoder.decode(ret,"UTF-8"));
		EntityUtils.consume(entity); // 释放是否可以去掉？
		response.close(); // 关闭资源,可以写在finally里
		httpGet.releaseConnection(); // 释放资源
		httpClient.close(); // 关闭资源
	}
}
